package com.ys.controller.api.finance;

import com.alibaba.fastjson.JSONObject;
import com.ys.entity.finance.TcwFinancesApiInFoId;
import com.ys.entity.finance.TcwFinancesEventGroups;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/inter/tcwFinancesApiInFoId")
public class ApiInterTcwFinancesApiInFoIdController {
	private static Long MENU_ID = 82L; // TODO

//	private static final Logger logger = Logger.getLogger(ApiAdminLoginController.class);
	
	@Autowired
	private CommonApiService<TcwFinancesApiInFoId> commonApiService;

	@RequiresPermissions("tcwFinancesApiInFo:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(@ModelAttribute TcwFinancesApiInFoId cond){
		cond
		.where("A.Type != 'Transfer'")
		.setOrderby("A.PurchaseDate desc");
		LayuiTableVoJA vo = commonApiService.searchLay(cond);
		{
			JSONObject JO = new JSONObject();
			// 订单总金额累计
			JO.put("total", cond.setFields("ISNULL(SUM(A.Total),0)").sumDecimal());

			vo.setTotalRow(JO);
		}
		return vo;
	}

	@RequestMapping("/export")
	@ResponseBody
	public R export(@ModelAttribute TcwFinancesApiInFoId cond, String conditions) throws Exception{
		cond
		.where("A.Type != 'Transfer'")
		.setOrderby("A.PurchaseDate desc")
		.setPage(0).setSize(999999);

		List<Map<String, Object>> listData = cond
				.queryMap();

		List<YsCol> listField = new YsCol()
				.setMenuId(MENU_ID)
				.setFrontShow(YsColEnum.FrontShow.SHOW.value)
				.setOrderby("A.SortNo asc")
				.queryList();


		ExportExcelUtils<TcwFinancesEventGroups> util = new ExportExcelUtils<TcwFinancesEventGroups>(new TcwFinancesEventGroups());

		String toFilePath = util.exportExcel(listData, listField, "财务事件表(按订单)");
		UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
		return R.ok(dto.getUrl());
	}
}
